/**
 * Created by jason on 2016/11/6.
 */

var React = require('react');
var ReactDom = require('react-dom');
//var Application = require('./module/application.js');//Application module

window.P2P_OBJ = {};//管理PeerConnection 数量
window.intervalTitle = null;
//应用程序主入口
var Application = React.createClass({
    getDefaultProps:function () {
        return {


        }

    },
    getInitialState:function () {
        return {
            hiddenLogin:false,//页面初始化显示登录页
            username:"ss",//当前用户名
            callState:"free",//通迅装态
            connectedUser:null,//当前正在通迅的用户名
            handleRefuse:false,//收到拒绝邀请
            isInviter:false,//当前用户是邀请方
            muted:false,//静音
            isHangUper:false,//是否是挂断电话的人
            showRemind:false,//是否显示提醒消息
            hangUpType:0,//拒绝通讯的类型   取值范围 0:还有有新通自己挂断,1:还没有接通对方挂断,2:已经接通自己挂断,3,已经接通对方挂断
            pageTitle:"芝麻开花聊天系统",//默认的页面头信息
            contacts:null,//当前联系人
            contactsAll:null,//所有联系人

        }
    },
    componentDidMount: function() {
        this.serverRequest = $.get(this.props.contactsJsonUrl, function (result) {
            console.log("result",result);
            if(production){
                var result = eval(result)['people'];
            }else{
                var result = JSON.parse(result)['people'];
            }


            console.log("result", typeof result);

            this.setState({
                contacts: result,
                contactsAll:result

            });


        }.bind(this));
        $("title").html(this.state.pageTitle);

    },


    componentWillUnmount: function() {
        this.serverRequest.abort();
    },
    componentWillReceiveProps:function (e) {
        console.log("receive new msg",e);
    },
    componentDidUpdate:function () {

        switch (this.state.callState){
            case "free":
                console.log("free");
                intervalChattingModule = null;
                document.getElementById("main").addEventListener("mousemove",function (e) {
                    console.log("have moved out!");

                });
                $("title").html(this.state.pageTitle);

                break;

            case "chatting":
                console.log("chatting");
                $("#remoteVideo").show();
                intervalChattingModule = function(){
                    setTimeout(function () {


                        $("#chatting-module").css("left","-280px");
                        $("#localVideo").removeClass("localVideo").addClass("remoteVideo");
                        $("#remoteVideo").removeClass("remoteVideo").addClass("localVideo");

                        document.getElementById("main").addEventListener("mousemove",function (e) {

                            if($("#chatting-module").css("left") == "-280px"){
                                console.log("have moved!");
                                $("#chatting-module").css("left","0px");
                                setTimeout(intervalChattingModule,5000);
                            }else{
                                //console.log("have!");

                            }

                        });

                    },2000);

                }
                intervalChattingModule();
                break;

            case "calling":
                console.log("calling");
                $("#calling-module").css("left","0");
                $("#remoteVideo").hide();
                break;

            case "handleCalling":
                console.log("handleCalling");
                $("#handle-calling-module").css("left","0");
                break;

        }


    },

    startCall:function (name,phone) {
        console.log(name,phone);
        console.log("username",this.state.username);

        if(name == this.state.username){
            alert("不能发送邀请给自已!");
            return;
        }

        this.setState({"callState":"calling","isInviter":true});
        console.log(name,phone);



        this.name=name = 13127952680;

        var __self = this;




        P2P.invite(name, function(){
            console.log('Invite success.');


            getLocalStream(function (localStream) {
                if($('#localVideo') && $('#localVideo').get(0)){
                    $('#localVideo').get(0).srcObject = localStream.mediaStream;
                    window.localStream = localStream;
                    console.log(localStream);
                    P2P.publish(localStream,__self.name);  // Publish local stream to remote client
                    __self.setState({"connectedUser":name});

                }
            })

        }, function(err){
            console.log('Invite failed with message: ' + err.message);
        });

    },
    acceptInvite:function () {
        console.log("this.state.username");
        //console.log("this.state.username",this.state.connectedUser);
        //P2P.accept(this.state.connectedUser);
        if(intervalTitle){
            clearInterval(intervalTitle);
        }
        P2P.publish(localStream,this.state.connectedUser);
        this.setState({callState:"chatting"});
    },
    hangUp:function (type) {//type的取值有四种如下 取值范围 0:还有有新通自己挂断,1:还没有接通对方挂断,2:已经接通自己挂断,3,已经接通对方挂断
        var __self = this;
        console.log("close type:",type);
        switch(type){
            case 0:
                __self.setState({callState:"free",isHangUper:true,hangUpType:0,connectedUser:null},afterHangUp);
                break;
            case 1:
                __self.setState({callState:"free",isHangUper:true,hangUpType:1,connectedUser:null},afterHangUp);
                break;
            case 2:
                __self.setState({callState:"free",isHangUper:true,hangUpType:2,connectedUser:null},afterHangUp);
                break;
            case 3:
                __self.setState({callState:"free",isHangUper:true,hangUpType:3,connectedUser:null},afterHangUp);
                break;
        }
        //挂断电话后的回调函数
        function afterHangUp(){
            P2P.stop(__self.state.connectedUser);  // Disconnected from peer server.

            if(localStream){
                localStream.close();
                localStream = undefined;
            }

            if(intervalTitle){
                clearInterval(intervalTitle);
            }
            __self.setState({isHangUper:false});
        }




    },
    closeRemind:function () {
        console.log("closeRemind");
        this.setState({showRemind:false});
    },
    disableAudio:function () {
        console.log("localStream",localStream);
        console.log("P2P:",P2P);
        if(!this.state.muted){
            localStream.disableAudio();
            //localStream.disableVideo();
            this.setState({"muted":true});
        }else{
            localStream.enableAudio();  // Disconnected from peer server.
            //localStream.enableVideo();  // Disconnected from peer server.
            this.setState({"muted":false});
        }
    },
    searchContact:function (e) {
        console.log(e.target.value);
        var searchVal =   e.target.value;
        var  contactsAll =  this.state.contactsAll;
        var curContact = [];
        console.log(contactsAll[0].name);

        for(var i =0;i<contactsAll.length;i++){
             if(searchVal == contactsAll[i].name){
                 curContact.push(contactsAll[i]);
             }
        }
        //console.log(curContact);
        this.setState({contacts:curContact});

    },

    Login:function (username) {
        var contacts = this.state.contacts;
        
        function hasThisUser(username,contacts) {
            for(var i =0 ;i<contacts.length;i++){
                if(contacts[i].name == username){
                    return true;
                }
            }
            return false;

        }


        if(username && username.length>0){
            this.setState({hiddenLogin:true,username:username});
            var ___self = this;

            var P2P = null; //创建一个PeerConnection对象
                P2P = new Woogeen.PeerClient({
                iceServers : [{
                        urls : "stun:139.196.106.6:3478"
                    },{
                        urls : ["turn:139.196.106.6:3478?transport=udp","turn:139.196.106.6:3478?transport=tcp"],
                        credential : "kurento",
                        username : "kurento"
                    }]
                })

                P2P.on('chat-invited',function(e){  // Receive invitation from remote client.
                    console.log("handle invited BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB",e.senderId);
                    console.log("callState",___self.state.callState);
                    if(___self.state.callState == "free"){
                        ___self.setState({callState:"handleCalling",connectedUser:e.senderId},function () {
                            getLocalStream(function (localStream) {
                                $('#localVideo').get(0).srcObject = localStream.mediaStream;
                                P2P.accept(e.senderId);
                            });
                            intervalTitle = setInterval(function () {
                                if($("title").text() == "收到新的视频邀请"){
                                    $("title").html("");
                                }else{
                                    $("title").html("收到新的视频邀请");
                                }

                            },1000);
                        });
                    }else{
                        P2P.deny(e.senderId);
                    }

                }.bind(this));

                P2P.on('chat-accepted', function(e){
                    console.log('chat-accepted from '+e.senderId+'.');
                    if(e.stream){
                        $('#remoteVideo').get(0).srcObject = e.stream.mediaStream;
                    }

                    //P2P.publish(localStream,e.senderId);  // Publish local stream to remote client
                });

                P2P.on('chat-denied',function(e){
                    console.log('Invitation to '+e.senderId+' has been denied.');
                    console.log('Invitation to '+e.senderId+' has been denied.对方忙碌。');
                    ___self.setState({callState:"free",isInviter:false,showRemind:true,hangUpType:1},function () {
                        setTimeout(function () {
                            ___self.setState({showRemind:false});
                        },5000)
                    });

                    if(localStream){
                        localStream.close();
                        localStream=undefined;
                    }

                });

                P2P.on('stream-added',function(e){  // A remote stream is available.
                    //this.setState({callState:"chatting",connectedUser:e.stream.from});


                    console.log("AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA",e.stream.from);
                   // var from = e.stream.from;
                    $('#remoteVideo').get(0).srcObject = e.stream.mediaStream;
                    if(___self.state.isInviter){
                        ___self.setState({"callState":"chatting"});
                    }
                    //P2P.publish(localStream,from);



                }.bind(this));

                P2P.on('data-received',function(event){  // Received data from datachannel.
                    //console.log("received data:",JSON.parse(event.data));
                    //window.e = e;

                    //$('#dataReceived').val(e.senderId+': '+JSON.parse(e.data));


                    // if(typeof event.data  == "Object"){
                    //     console.log("Object",event.data);
                    //
                    // }else{
                    //     console.log("String",event.data);
                    // }
                    // return;

                    try {
                        var message = JSON.parse(event.data);



                        switch (message.type) {
                            case "start":
                                currentFile = [];
                                currentFileSize = 0;
                                fileSize = message.fileSize;
                                currentFileMeta = message;
                                console.log("Receiving file",currentFileSize);
                                console.log("fileSize",fileSize);
                                break;
                            case "end":
                                console.log("end");
                                saveFile(currentFileMeta,currentFile);
                                break;
                        }
                    } catch (event) {

                        currentFile.push(atob(event.data));

                        currentFileSize += currentFile[currentFile.length - 1].length;

                        console.log("currentFileSize",currentFileSize);
                        var percentage = Math.floor((currentFileSize /fileSize) * 100);
                        //statusText.innerHTML = "Receiving... " + percentage + "%";
                    }
                    function base64ToBlob(b64Data, contentType) {
                        contentType = contentType || '';

                        var byteArrays = [], byteNumbers, slice;

                        for (var i = 0; i < b64Data.length; i++) {
                            slice = b64Data[i];

                            byteNumbers = new Array(slice.length);
                            for (var n = 0; n < slice.length; n++) {
                                byteNumbers[n] = slice.charCodeAt(n);
                            }

                            var byteArray = new Uint8Array(byteNumbers);

                            byteArrays.push(byteArray);
                        }

                        var blob = new Blob(byteArrays, {type: contentType});
                        return blob;
                    }

                    function saveFile(meta, data) {
                        var blob = base64ToBlob(data, meta.type);

                        var link = document.createElement('a');
                        link.href = window.URL.createObjectURL(blob);
                        link.download = meta.name;
                        link.click();
                    }







                });

                P2P.on('stream-removed',function(e){  // A remote stream is available.
                    var stream = e.stream;

                    console.log('Stream removed. Stream ID: '+e.stream.mediaStream.id);


                });

                P2P.on('chat-stopped',function(e){  // Chat stopped.
                    console.log('chat stopped.');
                    console.log('Chat stopped. Sender ID: '+e.senderId+', peer ID: '+e.peerId);



                    if(localStream){
                        localStream.close();
                        localStream=undefined;
                    }

                    if(___self.state.isHangUper){//挂断者
                        console.log("挂断者");
                        ___self.setState({callState:"free",isInviter:false});
                    }else{//被挂断者
                        console.log("被挂断者");

                        if(___self.state.callState == "chatting" || ___self.state.isInviter){//已经开始聊天
                            ___self.setState({callState:"free",isInviter:false,showRemind:true,hangUpType:3},function () {
                                setTimeout(function () {
                                    ___self.setState({showRemind:false,hangUpType:0,"connectedUser":null});
                                },5000);
                            });
                        }else{//还未开始聊天
                            ___self.setState({callState:"free",isInviter:false,"connectedUser":null});

                        }




                    }
                });

                P2P.on('chat-denied',function(e){
                    console.log('Invitation to '+e.senderId+' has been denied.');
                });



                window.P2P=P2P;


                P2P.connect({host:this.props.serverAddress,token:username});  // Connect to peer server


        }else{
            alert("请输入有效的用户名!");
        }

    },

    render:function () {

        if(this.state.hiddenLogin){//已经隐藏了登录
            return   <Main muted={this.state.muted} connectedUser={this.state.connectedUser} P2P={P2P} contacts={this.state.contacts} username={this.state.username} callState={this.state.callState}  startCall={this.startCall}  acceptInvite={this.acceptInvite} hangUp={this.hangUp}    refuseFun={this.refuseFun} disableAudio={this.disableAudio} showRemind={this.state.showRemind}  closeRemind={this.closeRemind}  hangUpType={this.state.hangUpType} searchContact={this.searchContact}  />

        }else{
            return   <Login Login={this.Login} serverAddress={this.serverAddress} />

        }

    }

})

//登录模块
var Login = React.createClass({
    getInitialState:function () {
        return {

        }
    },
    getDefaultProps:function () {
        return {
            title:"请输入手机号码"
        }
    },
    Login:function () {
        this.props.Login(this.refs.loginName.value);
    },

    render:function () {
        return  <div id="login_page">
                     <div className="login_main">
                         <div className="logo_wrap">
                             <img src="./build/images/logo.png" />
                         </div>
                         <div className="phone_number_wrap">
                             <input className="phone_number" type="text"  ref="loginName" placeholder={this.props.title} />
                         </div>
                         <div className="login_btn_wrap">
                             <button className="login_btn" onClick={this.Login}>确认登录</button>
                         </div>

                    </div>

                    <footer>Copyright 2015 OpenSesame Co.,Ltd. All Right Reserved  |  沪ICP备15048816号 </footer>

                </div>;

    }
})

//登录后的主模块
var Main = React.createClass({
    getInitialState:function () {
        return {
            dataSent:""
        }
    },
    componentWillReceiveProps:function () {
        console.log("forceUpdate");
        //this.forceUpdate();
    },
    handleChange: function(event) {
        this.setState({dataSent: event.target.value});
    },



    logOut:function (name,phone) {
        console.log(name,phone);
        this.props.P2P.unpublish(this.props.localStream,name);  // Publish local stream to remote client

    },

    sendData:function (val,to) {
        console.log(val,to);
        if(val){//有文本就发送文本
            this.props.P2P.send(val,to);  // Send data to peer.
        }else{//没广本就发送文件
            var files = document.getElementById("getFiles").files;
            console.log(files);console.log("files[0]",files[0]);
            var curfile = files[0];

            //curfile = JSON.stringify(curfile);


            var CHUNK_MAX = 16000;
            var ___self = this;
            function arrayBufferToBase64(buffer) {
                var binary = '';
                var bytes = new Uint8Array( buffer );
                var len = bytes.byteLength;
                for (var i = 0; i < len; i++) {
                    binary += String.fromCharCode( bytes[ i ] );
                }
                return btoa(binary);
            }
            function sendFile(file,P2P,to) {
                var reader = new FileReader();

                reader.onloadend = function(evt) {
                    if (evt.target.readyState == FileReader.DONE) {
                        var buffer = reader.result,
                            start = 0,
                            end = 0,
                            last = false;


                        function sendChunk() {
                            end = start + CHUNK_MAX;
                            console.log("end",end);

                            if (end > file.size) {
                                end = file.size;
                                last = true;
                            }

                            var percentage = Math.floor((end / file.size) * 100);
                            //statusText.innerHTML = "Sending... " + percentage + "%";
                            var arrayBufferStr = JSON.stringify(arrayBufferToBase64(buffer.slice(start, end)));

                            P2P.send(arrayBufferStr,to);

                            // If this is the last chunk send our end message, otherwise keep sending
                            if (last === true) {
                                console.log("send data end");
                                var end = JSON.stringify({
                                    type: "end"
                                });
                                P2P.send(end,to);
                            } else {
                                start = end;
                                // Throttle the sending to avoid flooding
                                setTimeout(function () {
                                    sendChunk();
                                }, 100);
                            }
                        }

                        sendChunk();
                    }
                };

                reader.readAsArrayBuffer(file);
            }

            var obj = {
                type: "start",
                fileSize:curfile.size,
                data: curfile
            };

            var osjt = JSON.stringify(obj);




            if (files.length > 0) {
                var P2P = this.props.P2P;
                P2P.send(osjt,to);

                sendFile(curfile,P2P,to);
            }


        }


    },
    componentDidMount:function(){
        //$('#localVideo').get(0).srcObject = "./build/images/main_1.jpg";
    },

    render:function () {
        var persons = this.props.contacts;


        switch (this.props.callState){
            case "handleCalling"://收到呼叫
                return  <div className="main">
                    <div id="handle-calling-module">
                        <h1 className="cur-calling-user">李马克</h1>
                        <section className="user-avatar-wrap">
                            <img className="user-avatar" src="build/images/avatar.png" />
                        </section>
                        <section className="calling-flash">
                            <img className="user-avatar" src="build/images/flush.gif" />
                        </section>
                        <section className="calling-text">
                            收到视频通话请求
                        </section>
                        <section className="hang-up-and-accept">
                            <ul className="f-oh">
                                <li className="pull-left"  onClick={this.props.hangUp.bind(null,1,event)}>
                                    <div className="hang-up-icon">
                                        <img className="hang-up" src="build/images/hang_up.png" />
                                    </div>
                                    <div>
                                        挂断
                                    </div>
                                </li>

                                <li className="pull-left" onClick={this.props.acceptInvite}>
                                    <div className="hang-up-icon">
                                        <img className="hang-up" src="build/images/accept-icon.png" />
                                    </div>
                                    <div >
                                        接受
                                    </div>
                                </li>
                            </ul>

                        </section>

                    </div>

                            <div id="video_container"><video   id="localVideo" className="localVideo"  autoPlay="autoplay" ></video><video   id="remoteVideo" className="remoteVideo" autoPlay="autoplay"></video></div>

                        </div>
                        break;
            case  "calling"://正在呼叫
                return <div className="main">
                    <div id="calling-module">
                        <h1 className="cur-calling-user">{this.props.username}</h1>
                        <section className="user-avatar-wrap">
                            <img className="user-avatar" src="build/images/avatar.png" />
                        </section>
                        <section className="calling-flash">
                            <img className="user-avatar" src="build/images/flush.gif" />
                        </section>
                        <section className="calling-text">
                            正在发送视频请求<br/>
                            请稍候
                        </section>
                        <section className="hang-up-icon-wrap" onClick={this.props.hangUp.bind(null,0,event)}>
                            <div className="hang-up-icon">
                                <img className="hang-up" src="build/images/hang_up.png" />
                            </div>
                            <div>
                                挂断
                            </div>
                        </section>

                    </div>

                            <div id="video_container"><video    id="localVideo" className="localVideo"  autoPlay="autoplay" ></video><video  id="remoteVideo" className="remoteVideo" autoPlay="autoplay"></video></div>

                        </div>
                        break;
            case "free"://待机状态
                var remindHtml;

                if(this.props.showRemind){//挂断者
                    switch (this.props.hangUpType){
                        case 0://还没有接通自己挂断
                            remindHtml = '<div></div>';
                            break;
                        case 1://还没有接通自己挂断
                            remindHtml = '<div class="refused">对方正在视频通话中<a class="pull-right close-remind-btn"><img src="build/images/close-remind.png" /></a></div>';
                            break;
                        case 2://还没有接通自己挂断
                            remindHtml = '<div class="refused">已经接通自己挂断<a class="pull-right close-remind-btn"><img src="build/images/close-remind.png" /></a></div>';
                            break;
                        case 3://还没有接通自己挂断
                            remindHtml = '<div class="refused">对方挂断<a class="pull-right close-remind-btn"><img src="build/images/close-remind.png" /></a></div>';
                            break;
                        default:
                            remindHtml = '<div class="refused">其它情况下的挂断<a class="pull-right close-remind-btn"><img src="build/images/close-remind.png" /></a></div>';

                    }

                }else{
                    remindHtml = '<div ></div>';
                }




                return <div className="main">


                                <div onClick={this.props.closeRemind} dangerouslySetInnerHTML={{__html:remindHtml}}></div>
                                <div id="contacts" className="content mCustomScrollbar">
                                    <h3 className="title">联系人列表</h3>
                                    <section className="search"  >
                                        <input type="text" onChange={this.props.searchContact} placeholder="按姓名或号码搜索" />
                                    </section>
                                    <ul className="contacts-ul">


                                        {
                                            persons.map(function (person) {

                                                return <li className="contacts-li" key={person.name.toString()}>
                                                    <div className="pull-left">
                                                        <img className="user_icon" src="build/images/user_icon.png" />
                                                    </div>
                                                    <div className="pull-left name-and-phone">
                                                        <b  className="name" name={person.name}>{person.name}</b>
                                                        <span className="phone">{person.phone}</span>
                                                    </div>
                                                    <div className="pull-left">
                                                        <a className="invite" onClick={this.props.startCall.bind(null,person.name,person.phone,event)}><img className="installed_icon" src="build/images/installed_icon.png" /></a>
                                                    </div>

                                                </li>
                                            }.bind(this))
                                        }
                                    </ul>
                                    <h5 className="invite-title">邀请</h5>
                                    <ul className="contacts-ul">
                                        {
                                            persons.map(function (person) {

                                                return <li className="contacts-li" key={person.name.toString()}>
                                                    <div className="pull-left">
                                                        <img className="user_icon" src="build/images/user_icon.png" />
                                                    </div>
                                                    <div className="pull-left name-and-phone">
                                                        <b  className="name" name={person.name}>姓名:{person.name}</b>
                                                        <span className="phone">电话:phone</span>
                                                    </div>
                                                    <div className="pull-left">
                                                        <a className="invite" onClick={this.props.startCall.bind(null,person.name,person.phone,event)}>邀请</a>
                                                    </div>

                                                </li>
                                            }.bind(this))
                                        }
                                    </ul>
                                </div>




                            <div id="main_realreal">
                                <div className="realreal-content-box">
                                    <ul>
                                        <li><img className="logo" src="build/images/logo.png" /></li>
                                        <li>RealReal.io</li>
                                        <li><b>流畅</b><span>•</span><b>高清</b><span>•</span><b>便捷</b><span>•</span><b>安全</b></li>
                                    </ul>

                                </div>
                            </div>



                        </div>

                break;
            case "chatting"://正在聊天
                if(this.props.muted){
                    var mutedImg="build/images/muted.png",mutedText="取消静音";

                }else{
                    var mutedImg="build/images/unmuted.png",mutedText="静音";
                }

                return <div className="main">
                    <div id="chatting-module">
                        <section className="hang-up-and-muted">
                            <ul className="f-oh">
                                <li>
                                    <div className="muted-wrap" onClick={this.props.disableAudio}>
                                        <img className="hang-up" src={mutedImg} />

                                        <span>
                                            {mutedText}
                                    </span>
                                    </div>

                                </li>
                                <li>
                                    <div className="hang-up-wrap" onClick={this.props.hangUp.bind(null,2,event)}>
                                        <img className="hang-up" src="build/images/hang_up.png" />

                                        <span>
                                            挂断
                                        </span>
                                    </div>


                                </li>


                            </ul>

                        </section>
                        </div>

                    <div id="video_container"><video    id="localVideo" className="localVideo"  autoPlay="autoplay" ></video><video id="remoteVideo" className="remoteVideo" autoPlay="autoplay"></video></div>



                    <div id="sendreceive" style={{display:"none"}}>
                        <h3>UserName:{this.props.username}</h3>
                        <div id="send">
                            <h2>Send data</h2>
                            send files:<input type="file" id="getFiles"  /> <br />
                            <textarea id="dataSent" onChange={this.handleChange} rows="5" cols="10"></textarea><br />
                            <button id="data-send" onClick={this.sendData.bind(null,this.state.dataSent,this.props.connectedUser,event)}>Send data</button>

                            <br />
                        </div>
                        <div id="receive">
                            <h2>Received data</h2>
                            <textarea id="dataReceived" rows="5" cols="10" disabled="true"></textarea>
                        </div>

                    </div>

                </div>
                break;



        }





    }
})



var config = {
    video:true,
    audio:true
}


var fileSize = null;//文件总大小
var  currentFile, currentFileSize, currentFileMeta;
var CHUNK_MAX = 16000;//一次发送的数据量大小


var localStream = null;//本地数据流

P2P_OBJ.connectedUser = null;//正在联系的人

var production = true;


var intervalChattingModule = null;
if(production){
    var contactsJsonUrl = "https://chat.i-sesame.com/react-webRTC/contacts.json";
    var serverAddress="https://chat.i-sesame.com:8096";//信令服务器地址

}else{
    var contactsJsonUrl = "contacts.json";
    var serverAddress="http://139.196.12.59:8095";//信令服务器地址

}



ReactDom.render(<Application  contactsJsonUrl={contactsJsonUrl}   serverAddress={serverAddress} needVideo={config.video}  />,document.getElementById('main'));

//获取本地视频流
function getLocalStream(callback) {
    Woogeen.LocalStream.create({
        video:{
            device:"camera",
            resolution:"hd1080p",
            frameRate: [60, 60]
        },
        //video:false,
        audio: true
    },function(err, stream){
        if (err) {
            return L.Logger.error('create LocalStream failed:', err);
        }

        localStream = stream;
        //window.stream = stream;
        if(callback){
            callback(localStream);
        }


    });
}


